package mil.af.amc.adpetracker.web.decorators;

import mil.af.amc.adpetracker.model.EquipmentItem;
import mil.af.amc.adpetracker.util.LinkBuilder;
import org.displaytag.decorator.TableDecorator;

import java.text.NumberFormat;

/**
 * Decorator class for EquipmentItem objects
 *
 * @author TSgt Jason Ferguson
 */

public class EquipmentItemDecorator extends TableDecorator {

    public String getSerialNumber() {

        EquipmentItem item = (EquipmentItem) getCurrentRowObject();

        LinkBuilder serialLink = new LinkBuilder();
        serialLink.setName(item.getSerial());
        serialLink.setBaseUrl("equipmentItemAction.do");
        serialLink.addParameter("method","viewItemPage");
        serialLink.addParameter("itemId", item.getId());

        return serialLink.getLink(LinkBuilder.LINK_TEXT, true);

        //return item.getSerial();
    }

    public String getManufacturer() {

        EquipmentItem item = (EquipmentItem) getCurrentRowObject();

        if (item.getItemModel().getManufacturer() == null) {
            return "";
        } else {
            return item.getItemModel().getManufacturer().getName();
        }
    }

    public String getModel() {
        EquipmentItem item = (EquipmentItem) getCurrentRowObject();

        // Deal with legal models until everything is updated
        if (item.getItemModel() == null) {
            return item.getItemModel().getName();
        }

        LinkBuilder modelLink = new LinkBuilder();
        modelLink.setName(item.getItemModel().getName());
        modelLink.setBaseUrl("equipmentItemAction.do");
        modelLink.addParameter("method", "viewEquipmentItemByModelPage");
        modelLink.addParameter("modelId", item.getItemModel().getModelId());

        return modelLink.getLink(LinkBuilder.LINK_TEXT, true);

        //return item.getModel();
    }

    public String getDescription() {
        EquipmentItem item = (EquipmentItem) getCurrentRowObject();
        return item.getDescription();
    }

    public String getEquipmentType() {
        EquipmentItem item = (EquipmentItem) getCurrentRowObject();
        return item.getItemModel().getType().getName();
    }

    public String getAssignedUser() {
        EquipmentItem item = (EquipmentItem) getCurrentRowObject();
        if (item.getAssignedUser() == null) {

            switch(item.getItemStatus()) {
                case Drmo:
                    return "DRMO";
                case PendingDrmo:
                    return "Pending DRMO";
                case Ros:
                    return "ROS";
                case PendingRos:
                    return "Pending ROS";
            }

            if (item.getLocation() != null) {
                return "Location: ".concat(item.getLocation());
            } else {
                return "";
            }
           
        } else {
            LinkBuilder viewUserLink = new LinkBuilder();
            viewUserLink.setName(item.getAssignedUser().getDisplayName());
            viewUserLink.setBaseUrl("userAction.do");
            viewUserLink.addParameter("method", "viewUserProfile");
            viewUserLink.addParameter("userId", item.getAssignedUser().getId());

            return viewUserLink.getLink(LinkBuilder.LINK_TEXT, true);
        }

    }

    public String getOriginalCost() {
        EquipmentItem item = (EquipmentItem) getCurrentRowObject();

        NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
        return currencyFormatter.format(item.getOriginalCost());

    }

    // Actions

    public String getUserProfileActions() {

        EquipmentItem item = (EquipmentItem) getCurrentRowObject();

        StringBuilder sb = new StringBuilder();
        LinkBuilder viewLink = new LinkBuilder();
        viewLink.setName("View Item");
        viewLink.setBaseUrl("equipmentItemAction.do");
        viewLink.addParameter("method", "viewItemPage");
        viewLink.addParameter("itemId", item.getId());

        sb.append(viewLink.getLink(LinkBuilder.LINK_TEXT, true));
        return sb.toString();
    }

    public String getActions() {

        EquipmentItem item = (EquipmentItem) getCurrentRowObject();

        StringBuilder sb = new StringBuilder();

        if (item.getAssignedUser() != null) {
            LinkBuilder unassignLink = new LinkBuilder();
            unassignLink.setName("Unassign");
            unassignLink.setBaseUrl("equipmentItemAction.do");
            unassignLink.addParameter("method", "unassignItem");
            unassignLink.addParameter("itemId", item.getId());
            sb.append(unassignLink.getLink(LinkBuilder.LINK_TEXT, true));
            sb.append(" ");
        }

        LinkBuilder viewLink = new LinkBuilder();
        viewLink.setName("View");
        viewLink.setBaseUrl("equipmentItemAction.do");
        viewLink.addParameter("method", "viewItemPage");
        viewLink.addParameter("itemId", item.getId());
        sb.append(viewLink.getLink(LinkBuilder.LINK_TEXT, true));
        sb.append(" ");

        LinkBuilder deleteLink = new LinkBuilder();
        deleteLink.setName("Edit");
        deleteLink.setBaseUrl("equipmentItemAction.do");
        deleteLink.addParameter("method", "viewEditItemPage");
        deleteLink.addParameter("itemId", item.getId());
        sb.append(deleteLink.getLink(LinkBuilder.LINK_TEXT, true));

        return sb.toString();

    }

    public String getUserProfilePageActions() {

        EquipmentItem item = (EquipmentItem) getCurrentRowObject();
        StringBuilder sb = new StringBuilder();

        LinkBuilder viewLink = new LinkBuilder();
        viewLink.setName("View");
        viewLink.setBaseUrl("equipmentItemAction.do");
        viewLink.addParameter("method", "viewItemPage");
        viewLink.addParameter("itemId", item.getId());
        sb.append(viewLink.getLink(LinkBuilder.LINK_TEXT, true));

        sb.append(" ");

        LinkBuilder unassignLink = new LinkBuilder();
        unassignLink.setName("Unassign");
        unassignLink.setBaseUrl("equipmentItemAction.do");
        unassignLink.addParameter("method", "unassignItem");
        unassignLink.addParameter("itemId", item.getId());
        sb.append(unassignLink.getLink(LinkBuilder.LINK_TEXT, true));

        return sb.toString();
    }

    public String getModelPageActions() {

        EquipmentItem item = (EquipmentItem) getCurrentRowObject();
        StringBuilder sb = new StringBuilder();

        LinkBuilder assignLink = new LinkBuilder();
        assignLink.setName("Assign");
        assignLink.setBaseUrl("equipmentItemAction.do");
        assignLink.addParameter("method","viewAssignEquipmentToUser");
        assignLink.addParameter("itemId", item.getId());
        sb.append(assignLink.getLink(LinkBuilder.LINK_TEXT, true));

        return sb.toString();

    }
}
